package com.tyrone.concurrent.basic;

/**
 * Thread.join  当前线程等待Thread 线程执行完之后才完成
 * eg:
 * A ：{
 *     B.join
 * }
 *
 * 执行顺序：    B-A
 */
public class Join {
    public static void main(String[] args) {
        Thread previous = Thread.currentThread();
        for (int i = 0; i < 10; i++) {
            Thread thread = new Thread(new Domino(previous), String.valueOf(i));
            thread.start();
            previous = thread;
        }
        System.out.println(Thread.currentThread().getName());
    }

    static class Domino implements Runnable{
        private Thread thread;
        public Domino(Thread thread){
            this.thread  = thread;
        }

        @Override
        public void run() {
            try {
                thread.join(2000);
            }catch (InterruptedException e){

            }
            System.out.println(Thread.currentThread().getName()+"    terminate.");
        }
    }
}
